<?php

/**
 * uniBoard ACP New BB Code
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_new_bbcode{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create API
		$this -> api = new api_bbcode( $appCore);
		
		// Update?
		if ( $appCore -> _POST( 'update') == 1)
		{
			// Fill Api
			$this -> api -> setBBCodeInEditor( $appCore -> _POST( 'bbcode_in_editor'));
			$this -> api -> setBBCodeName( $appCore -> _POST( 'bbcode_name'));
			$this -> api -> setBBCodeTag( $appCore -> _POST( 'bbcode_tag'));
			$this -> api -> setBBCodeType( $appCore -> _POST( 'bbcode_type'));
			$this -> api -> setBBCodeReplace( $appCore -> _POST( 'bbcode_replace'));
			$this -> api -> setBBCodeTemplate( $appCore -> _POST( 'bbcode_template'));
			$this -> api -> setBBCodeNoParse( $appCore -> _POST( 'bbcode_noparse'));
			$this -> api -> setBBCodeTrim( $appCore -> _POST( 'bbcode_trim'));
			$this -> api -> setBBCodeNl2Br( $appCore -> _POST( 'bbcode_nl2br'));
			
			// Api All-rite?
			if ( $this -> api -> error)
			{
				// Draw Errors
				$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> lang['acp_new_bbcode_form']);
				
				// Repeat Form
				$this -> drawForm();
			}
			else
			{
				// Save Api
				$this -> api -> saveData();
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_bbcode_added', htmlspecialchars( $this -> api -> bbcode_name));
				
				// Redirect us back to list
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=bbcodes'), $appCore -> lang['acp_bbcode_saved']);
			}
		}
		else
		{
			// Draw Form
			$this -> drawForm();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=new_bbcode'));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock();
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bbcode_form_name'], $this-> appCore -> drawTextInput( 'bbcode_name', $this -> api -> bbcode_name));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bbcode_form_in_editor'], $this-> appCore -> drawYesNo( 'bbcode_in_editor', $this -> api -> bbcode_in_editor));

		// New Section
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_bbcode_form_sect_write']);
		
		// Draw Fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bbcode_form_tag'], $this-> appCore -> drawTextInput( 'bbcode_tag', $this -> api -> bbcode_tag), $this -> appCore -> lang['acp_bbcode_form_tag_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bbcode_form_type'], $this-> appCore -> drawList( 'bbcode_type', array(
			0 => $this -> appCore -> lang['acp_bbcode_form_type_0'],
			1 => $this -> appCore -> lang['acp_bbcode_form_type_1'],
			2 => $this -> appCore -> lang['acp_bbcode_form_type_2']
		), $this -> api -> bbcode_type));
				
		// New Section
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_bbcode_form_sect_display']);
		
		// Draw Fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bbcode_form_replace'], $this-> appCore -> drawTextArea( 'bbcode_replace', $this -> api -> bbcode_replace), $this -> appCore -> lang['acp_bbcode_form_replace_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bbcode_form_template'], $this-> appCore -> drawTextInput( 'bbcode_template', $this -> api -> bbcode_template), $this -> appCore -> lang['acp_bbcode_form_template_expl']);
				
		// New Section
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_bbcode_form_sect_parsing']);
		
		// Draw Fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bbcode_form_in_noparse'], $this-> appCore -> drawYesNo( 'bbcode_noparse', $this -> api -> bbcode_noparse), $this -> appCore -> lang['acp_bbcode_form_in_noparse_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bbcode_form_in_trim'], $this-> appCore -> drawYesNo( 'bbcode_trim', $this -> api -> bbcode_trim), $this -> appCore -> lang['acp_bbcode_form_in_trim_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_bbcode_form_in_nl2br'], $this-> appCore -> drawYesNo( 'bbcode_nl2br', $this -> api -> bbcode_nl2br), $this -> appCore -> lang['acp_bbcode_form_in_nl2br_expl']);
		
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['acp_new_bbcode_form_submit']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['acp_new_bbcode_form'], array(
			$this -> appCore -> adminLink( 'act=bbcodes') => $this -> appCore -> lang['acp_bbcodes_custom'],
			'' => $this -> appCore -> lang['acp_new_bbcode_form']
		), ob_get_clean());
			
	}

}